/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306.bean;

import java.util.ArrayList;
import java.util.List;
import my12306.table.KeyValueItem;

/**
 * get the left tckets info
 * <pre>
 * left ticket format:
 * 10156536081015650790
 * O060100337M096200050P114500008
 * 1017753201405745004010177502583031550161
 * seat type(1), price(5),left(4)
 * </pre>
 *
 * @author Jack
 */
public class LeftTickets {

    private KeyValueItem seatType;
    private float price;
    private int left;

    public LeftTickets() {
    }

    public static List<LeftTickets> parse(String data) {
        int start = 0;
        List<LeftTickets> ticketInfo = new ArrayList<LeftTickets>();
        do {
            String tmp = data.substring(start, start + 10);
            start = start + 10;
            LeftTickets info = new LeftTickets();
            String seat = tmp.substring(0, 1);
            String priceStr = tmp.substring(1, 6);
            String leftStr = tmp.substring(7, 10);
            float tmpPrice = (float) (Integer.parseInt(priceStr) / 10.0);
            int tmpLeft = Integer.parseInt(leftStr);
            info.setSeat(Seats.getSeat(seat));
            info.setPrice(tmpPrice);
            info.setLeft(tmpLeft);
            ticketInfo.add(info);
        } while (start < data.length());
        if (ticketInfo.get(0).seatType.equals(Seats.SeatHard)) {
            ticketInfo.get(0).setSeat(Seats.SeatNone);
        }
        return ticketInfo;
    }

    /**
     * @return the seat
     */
    public KeyValueItem getSeat() {
        return seatType;
    }

    /**
     * @param seat the seat to set
     */
    public void setSeat(KeyValueItem seat) {
        this.seatType = seat;
    }

    /**
     * @return the price
     */
    public float getPrice() {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(float price) {
        this.price = price;
    }

    /**
     * @return the left
     */
    public int getLeft() {
        return left;
    }

    /**
     * @param left the left to set
     */
    public void setLeft(int left) {
        this.left = left;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[席别: ").append(seatType.toString()).append("]-")
                .append("[票价：").append(price).append("]-")
                .append("[剩余：").append(left).append("]");
        return sb.toString();
    }
}
